#ifndef JSCRIPTCONSOLE_H
#define JSCRIPTCONSOLE_H

#include "PythonQt.h"
#include <QVariant>
#include <QTextEdit>
#include <QCompleter>
#include <QTreeWidget>

class JScriptConsole : public QTextEdit
{
	Q_OBJECT

public:
	JScriptConsole(QWidget *parent, const PythonQtObjectPtr& context, Qt::WindowFlags i = 0);
	~JScriptConsole();

public slots:
	void executeLine();
	void keyPressEvent (QKeyEvent * e);
	void consoleMessage(const QString & message);
	QStringList history() { return mHistory; }
	void setHistory(const QStringList& h) { mHistory = h; mHistoryPosition = 0; }
	void clear();
	virtual void cut();
	void stdOut(const QString& s);
	void stdErr(const QString& s);
	void insertCompletion(const QString&);
	void appendCommandPrompt();
	QTreeWidget* getCommandList(void){ return mCommandList; }
	void flushOutput() {flushStdOut(); appendCommandPrompt();}

protected:
	void handleCompleterMenu();
	int  commandPromptPosition();
	bool verifySelectionBeforeDeletion();
	void setCurrentFont(const QColor& color = QColor(0,0,0), bool bold = false);
	void changeHistory();
	void flushStdOut();
	void paintEvent(QPaintEvent *);

private slots:
	void slotCursorPositionChanged();
	void slotViewportUpdate();

private:
	void executeCode(const QString& code);
	void braceMatching();

	PythonQtObjectPtr mContext;

	QStringList mHistory;
	int         mHistoryPosition;

	QString mClickedAnchor;
	QString mStorageKey;
	QString mCommandPrompt;
	QString mCurrentMultiLineCode;
	QString mStdOut;
	QString mStdErr;

	QTextCharFormat mDefaultTextCharacterFormat;
	QCompleter* mCompleter;
	QTreeWidget* mCommandList;

	QColor mLineColor;
};

#endif // JSCRIPTCONSOLE_H
